//
//  StartYQViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/1/14.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "StartYQViewController.h"
#import "ViewController.h"
#import "BaseInfoSecCell.h"
#import "ZPDetailViewController.h"
#import "ChosePositionViewController.h"
#import "HopeMoneyViewController.h"
#import "ChosePrivinceViewController.h"
#import "WorkYearsViewController.h"
#import "EducationViewController.h"
@interface StartYQViewController ()
<UITableViewDataSource,UITableViewDelegate,ChosePrivinceDelegae,HopeMoneyDelegate,workYearsDelegate,ChooseEducationDelegate>
{
    UITableView * _tableView;
    NSMutableArray * _dataSource;
    NSMutableArray * _dataArr;
    NSMutableArray * _idsArr;
    
    NSString * flag;
}
@end

@implementation StartYQViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
  // self.navigationController.navigationBarHidden = TRUE;

    //self.navigationItem.hidesBackButton = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self main];
    
    
   // self.navigationItem.title = @"我的职场轨迹";
    //[self addNavgationBar];
    
    [self addTitleLabel];
   [self addRightItem];
    // Do any additional setup after loading the view.
}
-(void)main
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectedZhiweiNotification:) name:@"selectedZhiweiNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SQZW:) name:@"SQZW" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(choosePSQNoti:) name:@"choosePSQNoti" object:nil];
    
    _idsArr = [[NSMutableArray alloc]init];
    _dataSource = [[NSMutableArray alloc]init];
    _dataArr = [[NSMutableArray alloc]init];
    [_idsArr addObjectsFromArray:@[@"",@"",@"",@"",@"",@""]];
    [_dataArr addObjectsFromArray:@[@"",@"",@"",@"",@"",@""]];
    [_dataSource addObjectsFromArray:@[@"职位",@"期望月资",@"期望城市",@"最高学历",@"工作年限",@"所在城市"]];
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [self createFooterView];
    [_tableView registerNib:[UINib nibWithNibName:@"BaseInfoSecCell" bundle:nil] forCellReuseIdentifier:@"BaseInfoSecCell"];
    _tableView.backgroundColor = UIColorFromRGB(0xf1f3f2);
    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}
-(void)loginwithclient:(NSString *)client username:(NSString *)username userpwd:(NSString *)userpwd
{
    __weak typeof(self)weakSelf = self;
    [U76HONet loginWithaction:@"" Client:client Username:username Userpwd:userpwd withLat:@"" withLong:@"" andDataBlock:^(LoginModel *model){
    
    
//    [U76HONet loginWithaction:@"" Client:client Username:username Userpwd:userpwd andDataBlock:^(LoginModel *model) {
        NSLog(@"登录成功了");
        //保存登录的code
        [Singleton share].code = model.code;
        [Singleton share].yqcode = model.qycode;
        [Singleton share].mobile = model.mobile;
        [Singleton share].password = _userpwd;
        [[Singleton share] saveUserInformation];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"modifyNameAndHeadpic" object:nil];
        [weakSelf.navigationController popViewControllerAnimated:NO];
    }];
}

#pragma mark - _tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BaseInfoSecCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BaseInfoSecCell"];
    cell.firstLab.text = _dataSource[indexPath.row];
    cell.secondLab.text = _dataArr[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ChosePositionViewController * chosepositionVC = [[ChosePositionViewController alloc]init];
        [self.navigationController pushViewController:chosepositionVC animated:YES];
    }
    else if (indexPath.row == 1)
    {
        HopeMoneyViewController * hopeMoneyVC = [[HopeMoneyViewController alloc]init];
        hopeMoneyVC.delegate = self;
        [self.navigationController pushViewController:hopeMoneyVC animated:YES];
    }
    else if (indexPath.row == 2)
    {
        ChosePrivinceViewController * choosePrivinceVC = [[ChosePrivinceViewController alloc]init];
        choosePrivinceVC.tag = (int)indexPath.row;
        choosePrivinceVC.delegate = self;
        [self.navigationController pushViewController:choosePrivinceVC animated:YES];
    }
    else if (indexPath.row == 3)
    {
        EducationViewController * chooseEducationVC = [[EducationViewController alloc]init];
        chooseEducationVC.delegate = self;
        [self.navigationController pushViewController:chooseEducationVC animated:YES];
    }
    else if (indexPath.row == 4)
    {
        WorkYearsViewController * workYearVC = [[WorkYearsViewController alloc]init];
        workYearVC.delegate = self;
        [self.navigationController pushViewController:workYearVC animated:YES];
    }
    else
    {
        ChosePrivinceViewController * choosePrinvinceVC = [[ChosePrivinceViewController alloc]init];
        choosePrinvinceVC.tag = (int)indexPath.row;
        choosePrinvinceVC.delegate = self;
        [self.navigationController pushViewController:choosePrinvinceVC animated:YES];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
#pragma mark - 自定义代理的实现
-(void)selectePrivince:(NSString *)privince andids:(NSString *)ids withTag:(int)tag
{
    [_dataArr replaceObjectAtIndex:tag withObject:privince];
    [_idsArr replaceObjectAtIndex:tag withObject:ids];
    [_tableView reloadData];
}
-(void)choseHopeMoneywithStr:(NSString *)hopemoneyStr andHopemoneyId:(NSString *)hopemoneyId
{
    [_dataArr replaceObjectAtIndex:1 withObject:hopemoneyStr];
    [_idsArr replaceObjectAtIndex:1 withObject:hopemoneyId];
    [_tableView reloadData];
}
-(void)choseWorkYearsWith:(NSString *)years andId:(NSString *)yearId
{
    [_dataArr replaceObjectAtIndex:4 withObject:years];
    [_idsArr replaceObjectAtIndex:4 withObject:yearId];
    [_tableView reloadData];

}
-(void)chooseEducationWithStr:(NSString *)education andEducationid:(NSString *)educationId
{
    [_dataArr replaceObjectAtIndex:3 withObject:education];
    [_idsArr replaceObjectAtIndex:3 withObject:educationId];
    [_tableView reloadData];
}
-(void)selectedZhiweiNotification:(NSNotification *)noti
{
    NSDictionary * dic = noti.object;
    [_dataArr replaceObjectAtIndex:0 withObject:[dic objectForKey:@"typenames"]];
    [_idsArr replaceObjectAtIndex:0 withObject:[dic objectForKey:@"ids"]];
    NSLog(@"登录时的消息通知");
    [_tableView reloadData];
}
-(void)SQZW:(NSNotification *)noti
{
    NSLog(@"收到消息了**************");
    flag = noti.object;
}
-(void)choosePSQNoti:(NSNotification *)noti
{
    NSDictionary * dic = noti.object;
    int tag = [[dic objectForKey:@"tag"] intValue];
    NSString * privince =[dic objectForKey:@"typenames"];
    NSString * ids = [dic objectForKey:@"ids"];
    [_dataArr replaceObjectAtIndex:tag withObject:privince];
    [_idsArr replaceObjectAtIndex:tag withObject:ids];
    [_tableView reloadData];
}
#pragma mark - createFooterView
-(UIView *)createFooterView
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SIZETO6H(98)+49)];
    view.backgroundColor = UIColorFromRGB(0xf1f3f2);
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(SIZETO6W(30), SIZETO6H(98), SCREEN_WIDTH-SIZETO6W(30)*2, 49);
    [btn setBackgroundColor:UIColorFromRGB(0x6ec844)];
    [btn setTitle:@"创建" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(createAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 20;
    [view addSubview:btn];
    return view;
}
-(void)createAction:(UIButton *)btn
{
    NSLog(@" 开始益企 ");
    __weak typeof(self)weakSelf = self;
    NSString * duty = _dataArr[0];
    NSString * salary = _dataArr[1];
    NSString * workcity = _dataArr[2];
    NSString * education = _dataArr[3];
    NSString * workyear = _dataArr[4];
    NSString * nowaddr = _dataArr[5];
//    [U76HONet registerUserWithaction:@"" client:_client username:_username userpwd:_userpwd Mobile:_mobile sex:@"" duty:duty salary:salary city:workcity education:education years:workyear address:nowaddr andDataBlock:^(NSDictionary *dic) {
//        NSLog(@"注册用户成功了");
//        [weakSelf loginwithclient:@"123654" username:_mobile userpwd:_userpwd];
//        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
//        
//    }];
    [U76HONet zhichangTrailWithAction:@"" code:_code duty:duty salary:salary city:workcity education:education years:workyear address:nowaddr andDataBlock:^(NSDictionary *dic) {
        NSLog(@"补充职场轨迹成功咧");
        [weakSelf loginwithclient:@"123654" username:_mobile userpwd:_userpwd];
        
       
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
    }];

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(void)addTitleLabel{

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-60, 30, 120, 30)];
    [self.view addSubview:label];
    label.text = @"我的职场轨迹";
    label.textAlignment = NSTextAlignmentCenter;
    
    

}
#pragma mark - addRightItem
-(void)addRightItem
{
    
    
    UIButton * scanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    scanBtn.frame = CGRectMake(self.view.frame.size.width-60, 30, 50, 30);
    [scanBtn setTitle:@"跳过" forState:UIControlStateNormal];
    [scanBtn addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    [scanBtn setTitleColor:UIColorFromRGB(0x6ec844) forState:UIControlStateNormal];
    
    [self.view addSubview:scanBtn];
    
    //[self.view addSubview:scanBtn];
//    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:scanBtn];
//    self.navigationItem.rightBarButtonItem = rightItem;
}

-(void)backItem:(id)sender
{
    __weak typeof(self)weakSelf = self;
    if ([flag isEqualToString:@"SQZW"]) {
        
        for (UIViewController *temp in weakSelf.navigationController.viewControllers) {
            if ([temp isKindOfClass:[ZPDetailViewController class]]) {
                [self loginwithclient:@"123654" username:_mobile userpwd:_userpwd];
                
                [self.navigationController popToViewController:temp animated:YES];
            }
        }

        
        
//        [U76HONet registerUserWithaction:@"" client:_client username:_username userpwd:_userpwd Mobile:_mobile sex:@"" duty:@"" salary:@"" city:@"" education:@"" years:@"" address:@"" andDataBlock:^(NSDictionary *dic) {
//            NSLog(@"注册用户成功了");
//            for (UIViewController *temp in weakSelf.navigationController.viewControllers) {
//                if ([temp isKindOfClass:[ZPDetailViewController class]]) {
//                    [weakSelf loginwithclient:@"123654" username:_mobile userpwd:_userpwd];
//                    [weakSelf.navigationController popToViewController:temp animated:YES];
//                }
//            }
//            
//        }];
        
    }
    else
    {
        [self loginwithclient:@"123654" username:_mobile userpwd:_userpwd];

        [self.navigationController popToRootViewControllerAnimated:YES];
//        [U76HONet registerUserWithaction:@"" client:_client username:_username userpwd:_userpwd Mobile:_mobile sex:@"" duty:@"" salary:@"" city:@"" education:@"" years:@"" address:@"" andDataBlock:^(NSDictionary *dic) {
//            NSLog(@"注册用户成功了");
//            [weakSelf loginwithclient:@"123654" username:_mobile userpwd:_userpwd];
//            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
//
//        }];
    }
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
